package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"login-master/models"
)

type LoginController struct {
	beego.Controller
}

func (c *LoginController) Add() {
	home := c.GetString("home")
	number, _ := c.GetInt("number")
	up := c.GetString("up")
	man := c.GetString("man")
	tel := c.GetString("tel")
	time := c.GetString("time")
	ll := models.Login{
		Home:   home,
		Number: number,
		Up:     up,
		Man:    man,
		Tel:    tel,
		Time:   time,
	}
	err := models.DB.Create(&ll).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
}
func (c *LoginController) Up() {
	id, _ := c.GetInt("id")
	home := c.GetString("home")
	number, _ := c.GetInt("number")
	up := c.GetString("up")
	man := c.GetString("man")
	tel := c.GetString("tel")
	time := c.GetString("time")
	ll := models.Login{
		Home:   home,
		Number: number,
		Up:     up,
		Man:    man,
		Tel:    tel,
		Time:   time,
	}
	err := models.DB.Where("id=?", id).Updates(&ll).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "修改失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "修改成功",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
}
func (c *LoginController) Del() {
	id, _ := c.GetInt("id")

	err := models.DB.Delete(&models.Login{}, id).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "删除失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "删除成功",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
}
func (c *LoginController) List() {
	var ll []models.Login
	err := models.DB.Find(&ll).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "查询成功",
			Data: ll,
		}
		c.ServeJSON()
		return
	}
}
